package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.stylesheets.LinkStyle;

import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/time")
public class TiemController {
    @Reference
    private MemberService memberService;


    @RequestMapping("/findAll")
        public Result findAll(@RequestParam("list") List<String> list) throws ParseException {
        Map<String, Object> map = new HashMap<>();
        List<String> months = new ArrayList<>();
        String s = list.get(0);
        //计算过去一年的12个月
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String str1 = list.get(0);
        String str2 = list.get(1);
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(sdf.parse(str1));
        aft.setTime(sdf.parse(str2));
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH) + 1;
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        int abs = Math.abs(month + result);


        Date sDate = sdf.parse(str1);
        Calendar c = Calendar.getInstance();
        c.setTime(sDate);

        for (int i = 0; i < abs; i++) {
            Date date = c.getTime();
            months.add(new SimpleDateFormat("yyyy.MM").format(date));
            c.add(Calendar.MONTH, 1);//每次加一个月
        }
        map.put("months", months);

        List<Integer> memberCount = memberService.findMemberCountByMonths(months);
        map.put("memberCount", memberCount);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
    }

//    public static void main(String[] args) throws ParseException {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//
//        Date sDate = sdf.parse("2020-10-1");
//        System.out.println("String类型转Date类型 "+sDate); //要实现日期+1 需要String转成Date类型
//
//
//        Calendar c = Calendar.getInstance();
//        c.setTime(sDate);
//        c.add(Calendar.MONTH, 1);//利用Calendar 实现 Date日期+1天
//        Date date = c.getTime();
//        String format = new SimpleDateFormat("yyyy-MM").format(date);
//
//
//
//
//        System.out.println(format);



//    }
}
